package com.imax.emap.overlay
{
	import com.imax.emap.core.Bounds;
	import com.imax.emap.manager.MarkerManager;
	import com.imax.emap.projection.IProjection;
	
	import flash.geom.Point;
	
	import mx.core.IFactory;
	import mx.core.UIComponent;

	public class MarkerLayerOverlay extends UIComponent implements IOverlay
	{
		private var projection:IProjection;
		//private var markerManager:MarkerManager;
		private var zoom:int;
		private var _itemRenderer:IFactory;
		
		public function MarkerLayerOverlay(_projection:IProjection)
		{
			super();
			//markerManager = new MarkerManager(this);
			projection = _projection;
		}

		public function viewBoundChanged(_viewBound:Bounds, _zoom:int):void
		{
			if(zoom == _zoom) return;
			else{
				zoom = _zoom;
				invalidateDisplayList();
			}
		}
		
		public function getMarkerManager():MarkerManager{
			//return markerManager;
			return null;
		}
		
		/**
		 * 添加标注 
		 * @param 标注的数组，类型为Marker
		 * 
		 */		
		public function addMarker(marker:Marker):void{
			if( !this.contains(marker) ) this.addChild(marker);
			
		}
		
		/**
		 * 删除指定的标注 
		 * @param 标注的数组，类型为Marker
		 * 
		 */		
		public function removeMarker(marker:Marker):void{
			if(this.contains(marker)) this.removeChild(marker);
			
			//invalidateDisplayList();
		}
		
		
		/**
		 * 删除所有标注 
		 * 
		 */		
		public function clearMarkers():void{
			while(this.numChildren!=0){
				this.removeChildAt(0);
			}
			//invalidateDisplayList();
		}
		
		/**
		 * 
		 * @return 返回Marker类
		 * 
		 */		
		public function getMarkers():Array{
			var markers:Array = new Array();
			for(var i:int = 0; i < this.numChildren;i++) {
				markers.push(this.getChildAt(i));
			}
			return markers;
		}
		
		/**
		 * 更新视图，显示所有图片并设定每张图片的像素坐标
		 * @param unscaledWidth
		 * @param unscaledHeight
		 * 
		 */		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			var point:Point = new Point();
			for(var i:int = 0; i < this.numChildren;i++) {
				var marker:Marker = Marker(this.getChildAt(i));
				
				point = projection.fromLatLonToPixel(marker.latLon,zoom);
				marker.x = point.x - marker.myIconOffset.x;
				marker.y = point.y - marker.myIconOffset.y;
				//marker.x = point.x;
				//marker.y = point.y;
			}
		}
	}
}